//
//  STBaseCollectionView.m
//  赛拓
//
//  Created by 王磊 on 7/28/16.
//  Copyright © 2016 立金所. All rights reserved.
//

#import "STBaseCollectionView.h"

#import "STBaseCollectionViewCell.h"

@interface STBaseCollectionView()<UICollectionViewDelegate ,UICollectionViewDataSource ,STBaseViewDelegate>


@end
static NSString *commodityCellIdentifier = @"collection_cell";

@implementation STBaseCollectionView
+ (instancetype)instanceWithcollectionViewLayout:(UICollectionViewLayout *)layout {
    return [[self alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
}

+ (instancetype)instanceWithFrame:(CGRect)frame {
    
    return [[self alloc]initWithFrame:frame collectionViewLayout:[STBaseCollectionLayout instance]];
}
+ (instancetype)instance {
    
    return [[self alloc]initWithFrame:CGRectZero collectionViewLayout:[STBaseCollectionLayout instance]];
}
- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {
    if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
        
        _layout = (STBaseCollectionLayout *)layout;
        
        [self commitInit];
    }
    return self;
}

- (void)commitInit {
    self.dataSource = self;
    
    self.delegate = self;
    
    self.backgroundColor = ST_CLEAR_COLOR;
    
    self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
    [self registerClass:[STBaseCollectionViewCell class] forCellWithReuseIdentifier:commodityCellIdentifier];
    
    self.showsVerticalScrollIndicator = false;
    
    self.showsHorizontalScrollIndicator = false;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    STBaseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:commodityCellIdentifier forIndexPath:indexPath];
    
    cell.mDelegate = self;
    
    [cell updateData:self.dataArray[indexPath.row]];
    
    return cell;
}

#pragma mark --- collection view delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:true];
    if (self.mDelegate && [self.mDelegate respondsToSelector:@selector(onItemClick:withData:forIndexPath:)]) {
        
        [self.mDelegate onItemClick:STItemClickTypeItem withData:self.dataArray[indexPath.row] forIndexPath:indexPath];
    }
}
#pragma mark ---- 数据处理

- (void)firstLoadData:(id)data {
    [self.dataArray addObjectsFromArray:data];
    
    [self reloadData];
}
- (void)updateData:(id)data {
    if (self.dataArray.count) {
        [self.dataArray removeAllObjects];
    }
    [self.dataArray addObjectsFromArray:data];
    
    [self reloadData];
}

- (void)updateData:(id)data forIndexPath:(NSIndexPath *)indexPath {
    [self.dataArray replaceObjectAtIndex:indexPath.row withObject:data];
    
}
- (void)appendData:(id)data {
    [self.dataArray addObjectsFromArray:data];
    
    [self reloadData];
}
- (void)updateFooter:(id)date {
    
}
- (void)updateHeader:(id)data {
    
}
#pragma mark --- cell delegate
- (void)onItemClick:(STItemClickType)itemType withData:(id)data forIndexPath:(NSIndexPath *)indexPath {
    if (self.mDelegate && [self.mDelegate respondsToSelector:@selector(onItemClick:withData:forIndexPath:)]) {
        [self.mDelegate onItemClick:itemType withData:data forIndexPath:indexPath];
    }
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
    if (self.mDelegate && [self.mDelegate respondsToSelector:@selector(onItemClick:withData:forIndexPath:)]) {
        [self.mDelegate onItemClick:STItemClickTypeScroll withData:nil forIndexPath:nil];
        
    }
}

@end
